<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');

class Index extends Layout {

    protected $objAlert;
    protected $dtgSetting;
    protected $txtValue, $btnCancel;
    protected $intId;

    protected function Form_Run() {
        parent::Form_Run();

        if (!$this->objCurrentUser->IsAdmin)
            QApplication::Redirect('../error/forbidden.php');
    }

    protected function Form_PreRender() {
        parent::Form_PreRender();

        $this->dtgSetting->Refresh();
    }

    protected function Form_Create() {
        parent::Form_Create();

        $this->strContextMenu = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/setting/menu/index.tpl.php';
        
        $this->objAlert = new Alert($this);

        $this->dtgSetting = new SettingDataGrid($this);
        $this->dtgSetting->ShowFilter = false;
        $this->dtgSetting->ShowFooter = true;

        $this->dtgSetting->Paginator = $this->dtgSetting->PaginatorAlternate = new QPaginator($this->dtgSetting);
        $this->dtgSetting->ItemsPerPage = 10;

        $this->dtgSetting->MetaAddColumn(QQN::Setting()->Key);

        $this->dtgSetting->AddColumn(new QDataGridColumn('Value', '<?=$_FORM->dtgSetting_ValueColumnRender($_ITEM) ?>', 'HtmlEntities=false', array('OrderByClause' => QQ::OrderBy(QQN::Setting()->Value), 'ReverseOrderByClause' => QQ::OrderBy(QQN::Setting()->Value, false))));
        $this->dtgSetting->MetaAddColumn(QQN::Setting()->Description);
        $this->dtgSetting->AddColumn(new QDataGridColumn(null, '<?=$_FORM->dtgSetting_ActionColumnRender($_ITEM)?>', 'HtmlEntities=false'));

        $this->txtValue = new QTextBox($this->dtgSetting);
        $this->txtValue->Required = true;
        $this->txtValue->LabelForRequiredUnnamed = QApplication::Translate('Value is required');
        $this->txtValue->MaxLength = 50;
        $this->txtValue->AddAction(new QEnterKeyEvent(), new QAjaxAction('btnSave_Click'));
        $this->txtValue->AddAction(new QEnterKeyEvent(), new QTerminateAction());

        $this->btnCancel = new QLinkButton($this->dtgSetting);
        $this->btnCancel->Text = QApplication::Translate('Cancel');
        $this->btnCancel->SetCustomStyle('margin-left', '10px');
        $this->btnCancel->AddAction(new QClickEvent(), new QAjaxAction('btnCancel_Click'));
        $this->btnCancel->AddAction(new QClickEvent(), new QTerminateAction());
    }

    public function dtgSetting_ValueColumnRender(Setting $objSetting) {

        if (($objSetting->Id == $this->intId) || (($this->intId == -1) && (!$objSetting->Id)))
            return $this->txtValue->RenderWithError(false);
        else
            return QApplication::HtmlEntities($objSetting->Value);
    }

    public function dtgSetting_ActionColumnRender(Setting $objSetting) {

        if (($objSetting->Id == $this->intId) || (($this->intId == -1) && (!$objSetting->Id))) {

            $strControlId = 'btnSave' . $objSetting->Id;

            $btnSave = $this->GetControl($strControlId);
            if (!$btnSave) {
                $btnSave = new QLinkButton($this->dtgSetting, $strControlId);
                $btnSave->Text = QApplication::Translate('Save');
                $btnSave->CausesValidation = true;
                $btnSave->ActionParameter = $objSetting->Id;
                $btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));
                $btnSave->AddAction(new QClickEvent(), new QTerminateAction());
            }

            return $btnSave->Render(false) . $this->btnCancel->Render(false, 'CssClass="muted"');
        } else {

            $strControlId = 'btnEdit' . $objSetting->Id;

            $btnEdit = $this->GetControl($strControlId);
            if (!$btnEdit) {
                $btnEdit = new QLinkButton($this->dtgSetting, $strControlId);
                $btnEdit->Text = QApplication::Translate('Edit');
                $btnEdit->ActionParameter = $objSetting->Id;
                $btnEdit->AddAction(new QClickEvent(), new QAjaxAction('btnEdit_Click'));
                $btnEdit->AddAction(new QClickEvent(), new QTerminateAction());
            }

            if ($this->intId)
                $btnEdit->Enabled = false;
            else
                $btnEdit->Enabled = true;

            return $btnEdit->Render(false, 'CssClass="text-info"', 'HtmlEntities="false"');
        }
    }

    protected function btnEdit_Click($strFormId, $strControlId, $strParameter) {

        $this->intId = $strParameter;

        $objSetting = Setting::Load($this->intId);

        $this->txtValue->Text = $objSetting->Value;
        
        $objFocusControlAction = new QFocusControlAction($this->txtValue);
        QApplication::ExecuteJavaScript($objFocusControlAction->RenderScript($this->txtValue));
    }

    protected function btnSave_Click($strFormId, $strControlId, $strParameter) {

        try {
            $this->intId = $strParameter;
            
            $objSetting = Setting::Load($this->intId);
            $objSetting->Value = trim($this->txtValue->Text);

            $objSetting->LogUser = $this->objCurrentUser->Username;
            $objSetting->Save();

            $this->intId = null;
            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate('Setting Saved Successful !'));
        } catch (QCallerException $objExc) {
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }
    }

    protected function btnCancel_Click($strFormId, $strControlId, $strParameter) {

        $this->intId = null;
    }

}

Index::Run('Index', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/setting/index.tpl.php');
?>